//获取应用实例
var app = getApp();
var index = 1;
var userGuids = '';
var isAdmins = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hasMore: true,
    // 是否显示loading
    showLoading: false,
    // loading提示语
    loadingMessage: '',
    // 是否显示toast
    showToast: false,
    // 提示消息
    toastMessage: '',
    keyword: '',
    IsAdmin:'管理员',
  },
  // 显示loading提示
  showLoading(loadingMessage) {
    this.setData({
      showLoading: true,
      loadingMessage
    });
  },

  // 隐藏loading提示
  hideLoading() {
    this.setData({
      showLoading: false,
      loadingMessage: ''
    });
  },

  // 显示toast消息
  showToast(toastMessage) {
    this.setData({
      showToast: true,
      toastMessage
    });
  },

  // 隐藏toast消息
  hideToast() {
    this.setData({
      showToast: false,
      toastMessage: ''
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var self = this;
    // this.showLoading('加载中...');
    index = 1;
    getData(self);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    getData(that);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (!that.data.showLoading) {
      getData(that);
    }
  },
  // 小程序 点击搜索 按钮时  事件
  confirmTap: function(event) {
    console.log(event.detail.value);
    this.setData({
      keyword: event.detail.value
    });
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    getData(that);
  },
  setAdmin: function(event) {
    let that = this;
    console.log(event);
    // 取得下标
    var index = parseInt(event.currentTarget.id);
    // 取出id值
    userGuids = that.data.list[index].userGuid;
    isAdmins = that.data.list[index].isAdmin;
    console.log(userGuids);
    wx.showModal({
      title: '提示',
      content: '确定设置用户为管理员？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')

          if (isAdmins == 1) {
            setadmin(userGuids, "0",that);
          } else {
            setadmin(userGuids, "1",that);
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})

var setadmin = function(userGuid, isAdmin,that) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/setAdmin', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userGuid: userGuid,
        isAdmin: isAdmin
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function(res) {
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '设置用户成功！',
          icon: 'success',
          duration: 1000
        })
        var self = that;
        // this.showLoading('加载中...');
        index = 1;
        getData(self);
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}

//获取数据
var getData = function(self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/getAllUser', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userName: self.data.keyword,
        index: index,
        currentPageCount: 20
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function(res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      self.hideLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.list.length == 0) {
          self.setData({
            hasMore: false,
          })
        } else {
          if (res.data.UserArea.list.length < 20) {
            self.setData({
              hasMore: false,
            })
          }
          if (index == 1) {
            self.setData({
              list: res.data.UserArea.list
            });
          } else {
            self.setData({
              list: self.data.list.concat(res.data.UserArea.list)
            });
          }
        }
        index++;
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    },

  })
}